/**
 * 
 */

// コンストラクタ
var Ball = function() {
	// 画像index
	this.img_index = 0;
	// 描画オブジェクト
	this.img = null;
	
	// 座標位置(左上)
	this.x = 0;
	this.y =　0;
	
	// 前回の座標位置(左上)pはprevの意
	this.px = 0;
	this.py = 0;
	
	// 移動ベクトル(単位円)
	this.vx = 0;
	this.vy = 0;
	
	// 移動パターン
	this.pattern = 0;
	
	// ボールサイズ
	this.r =　2;
	
	// スピード・威力
	this.speed = 5;
	this.power =　1;
	
	// 当たり判定用
	this.hit_obj = null;
	this.range_min = -1;
	this.hit_pos = false;
	this.hit_i = -1;
	this.hit_speed;
	// その他ステータス
	this.type = OBJ_TYPE_BALL;
	this.target_type = OBJ_TYPE_STAGE | OBJ_TYPE_BAR | OBJ_TYPE_BLOCK;
	this.state = 0;
	this.is_main = false;
	this.is_hit = false;
}

Ball.prototype.append = function(stage) {
	if (this.img == null) {
		switch(this.img_index) {
		case 0:
			this.r = 10;
			this.img = JSGF.factory(JSGF.Sprite, ["../image/blue1.png", "../image/blue2.png", "../image/blue3.png", "../image/blue4.png", ], this.r*2,this.r*2);
			this.img.startAnimation(100);
			this.img.move(this.x,this.y);
			break;
		default:
			break;
		}
	}
	stage.appendSprite(this.img);
}

Ball.prototype.setPos = function(x, y) {
	this.x = x;
	this.y = y;
}

Ball.prototype.setVec = function(vx, vy) {
	var div = vx * vx + vy * vy;
	div =　Math.sqrt(div);
	if (div == 0) {
		this.vx = 0;
		this.vy = 0;
	} else {
		this.vx = vx / div;
		this.vy = vy / div;
	}
}

Ball.prototype.addVec = function(vx, vy) {
	var coe = this.power * this.speed;//coefficient(係数)
	var nvx = this.vx * coe + vx;
	var nvy = this.vy * coe + vy;
//	this.speed = Math.sqrt(nvx * nvx + nvy + nvy);
	this.setVec(nvx, nvy);
}

Ball.prototype.action = function() {
	// 当たり判定の初期化
	this.hit_obj = null;
	this.range_min = -1;
	this.hit_pos = false;
	this.is_hit = false;
	this.hit_i = -1;
	this.hit_speed = this.speed;
	// 前回の迷子の応急処置
	if (this.px < 0) {this.x =　0; this.addVec(0.1, 0);}
	if (this.px > SCREEN_WIDTH) {this.x = SCREEN_WIDTH-this.r*2; this.addVec(-0.1, 0);}
	if (this.py < 0) {this.y = 0; this.addVec(0, 0.1);}
	if (this.py > SCREEN_HEIGHT) {this.y = SCREEN_HEIGHT-this.r*2; this.addVec(0, -0.1);}
	//
	this.px = this.x;
	this.py = this.y;
	switch(this.pattern) {
		case 0:	// 直線運動
			this.x += this.vx * this.speed;
			this.y += this.vy * this.speed;
			break;
		default:
			break;
	}
}
Ball.prototype.callback_action = function() {
	//
	switch(this.pattern) {
		case 0:	// 直線運動
			var len =	(this.hit_obj.hit_pos.x - this.px) *
						(this.hit_obj.hit_pos.x - this.px) +
						(this.hit_obj.hit_pos.y - this.py) *
						(this.hit_obj.hit_pos.y - this.py);
			len = Math.sqrt(len);
			this.px = this.x = this.hit_obj.hit_pos.x;
			this.py = this.y = this.hit_obj.hit_pos.y;
			this.hit_speed -= len;
			this.x += this.vx * this.hit_speed;
			this.y += this.vy * this.hit_speed;
			break;
		default:
			break;
	}
}
Ball.prototype.drawing = function() {
	this.img.move(this.x, this.y);
}

Ball.prototype.hitcheck = function(target) {
	switch(target.type) {
	case OBJ_TYPE_STAGE:		// 円と線の当たり判定 × 4
		if (	this.x < 0 ||					// 左
				this.x+this.r*2 > target.w ||	// 右
				this.y < 0 ||					// 上
				this.y+this.r*2 > target.h)		// 下
				{ 
					this.check_target(target);
					return true;
				}
		else	{return false;}
	case OBJ_TYPE_BAR:			// 線(円)と矩形の当たり判定
	case OBJ_TYPE_BLOCK:
		var wide = (target.type == OBJ_TYPE_BAR)?target.wide:1;
		if (this.px+this.r*2 < target.x && this.x+this.r*2 < target.x) return false;
		if (this.px > target.x+target.w*wide && this.x > target.x+target.w*wide) return false;
		if (this.py+this.r*2 < target.y && this.y+this.r*2 < target.y) return false;
		if (this.py > target.y+target.h && this.y > target.y+target.h) return false;
		var prevPos, nowPos;
		prevPos = new Pos(this.px+this.r, this.py+this.r);
		nowPos = new Pos(this.x+this.r, this.y+this.r);
		var rectVec = new Array();
		rectVec.push(new Pos(target.x, target.y));
		rectVec.push(new Pos(target.x, target.y + target.h));
		rectVec.push(new Pos(target.x + target.w*wide, target.y + target.h));
		rectVec.push(new Pos(target.x + target.w*wide, target.y));
		var is_hit = false;
		for(var i = 0; i < 4; i++) {
			this.hit_pos = hc_line_line(prevPos, nowPos, rectVec[i], rectVec[(i+1)%4], this.r);
			if (this.hit_pos) {
				this.hit_pos.x -= this.r;
				this.hit_pos.y -= this.r;
				this.hit_i =　i;
				this.check_target(target);
				is_hit = true;
			}
		}
		return is_hit;
	default:
		return false;
	}
}

Ball.prototype.hit_callback = function(target, is_subject) {
	switch(target.type) {
	case OBJ_TYPE_STAGE:
		// 反射させます
		if (this.x < 0) {this.vx *= -1;this.x *= -1;}
		else if(this.x+this.r*2 > target.w) {this.vx *= -1;this.x = this.x - (this.x+this.r*2-target.w)*2;}
		if (this.y < 0) {this.vy *= -1;this.y *= -1;}
		else if(this.y+this.r*2 > target.h) {this.vy *= -1;this.y = this.y - (this.y+this.r*2-target.h)*2;}
		
		break;
	case OBJ_TYPE_BAR:
	case OBJ_TYPE_BLOCK:
		// ベクトル修正
		if (this.hit_obj.i == 0 || this.hit_obj.i ==　2) {this.vx *=　-1;}
		else {this.vy *=　-1;}
		this.callback_action();
		
		break;
	default:
		break;
	}
	return;
}

Ball.prototype.check_target = function(target) {
	if (target.type == OBJ_TYPE_STAGE) {
		var len = 1;		// TODO 仮、基本的に最優先になるように
	} else { 
		var len =	(this.hit_pos.x - this.px) * (this.hit_pos.x - this.px) +
					(this.hit_pos.y - this.py) * (this.hit_pos.y - this.py);
	}
//	if (len < 1) return;
	
	if (len < this.range_min || this.range_min == -1) {
		this.range_min = len;
		this.hit_obj = target;
		this.hit_obj.i = this.hit_i;
		this.hit_obj.hit_pos = this.hit_pos;
	} else if (len ==　this.range_min) {
		if (this.hit_pos.s < this.hit_obj.hit_pos.s) {
			this.range_min = len;
			this.hit_obj = target;
			this.hit_obj.i = this.hit_i;
			this.hit_obj.hit_pos = this.hit_pos;
		}
	}
}